package com.wing.purchase.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.product.service.DynamicService;
import com.wing.purchase.dao.PurchaseEnquiryDao;
import com.wing.purchase.dao.PurchaseRequisitionsDao;
import com.wing.purchase.dao.PurchaseRequisitionsDetailDao;
import com.wing.purchase.model.entity.PurchaseEnquiry;
import com.wing.purchase.model.entity.PurchaseEnquiryDetail;
import com.wing.purchase.model.entity.PurchaseRequisitions;
import com.wing.purchase.model.entity.PurchaseRequisitionsDetail;
import com.wing.purchase.model.request.PurchaseEnquiryForm;
import com.wing.purchase.model.response.PurchaseEnquiryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 询价单-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class PurchaseEnquiryServiceImpl extends ServiceImpl<PurchaseEnquiryDao, PurchaseEnquiry> implements PurchaseEnquiryService {

    @Autowired
    private DynamicService dynamicService;
    @Autowired
    private PurchaseRequisitionsService purchaseRequisitionsService;
    @Autowired
    private PurchaseEnquiryDetailService purchaseEnquiryDetailService;
    @Autowired
    private PurchaseRequisitionsDetailService purchaseRequisitionsDetailService;

    @Override
    @Transactional(readOnly = false)
    public int add(PurchaseEnquiryForm purchaseEnquiryForm) {

        //校验

        PurchaseEnquiry purchaseEnquiry =new PurchaseEnquiry();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(PurchaseEnquiry.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(purchaseEnquiryForm, purchaseEnquiry, copyOptions);

        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        String code = dynamicService.getMaxCode("inquiry_no", "pur_purchase_enquiry");
        String day = simpleDateFormat.format(new Date());
        purchaseEnquiry.setInquiryNo("PE"+day+code);
        int i = baseMapper.insert(purchaseEnquiry);
        if(i > 0){
            // 修改请购单状态
            PurchaseRequisitions purchaseRequisitions = purchaseRequisitionsService.getById(purchaseEnquiry.getPurchaseRequisitionsId());
            if(purchaseRequisitions.getEnquiryStatus().equals(PurchaseRequisitions.EnquiryStatus.stayGenerateInquiry)){
                purchaseRequisitions.setEnquiryStatus(PurchaseRequisitions.EnquiryStatus.inquiry);
                purchaseRequisitionsService.updateById(purchaseRequisitions);
            }
            /**
             * 1--添加询价单明细
             * 2--判断请购单明细是否有id -- 请购单详情相关 -- 如果有id就修改为已生单
             */
            List<PurchaseRequisitionsDetail> purchaseRequisitionsDetails = new ArrayList<>();
            for (PurchaseEnquiryDetail purchaseEnquiryDetail: purchaseEnquiryForm.getPurchaseEnquiryDetails()) {
                purchaseEnquiryDetail.setPurchaseEnquiryId(purchaseEnquiry.getId());
                if(null != purchaseEnquiryDetail.getPurchaseRequisitionsDetailId()){ //修改请购单明细
                    PurchaseRequisitionsDetail detail = new PurchaseRequisitionsDetail();
                    detail.setId(purchaseEnquiryDetail.getPurchaseRequisitionsDetailId());
                    detail.setUpdateTime(LocalDateTime.now());
                    detail.setHasGenerateOrder(true);
                    purchaseRequisitionsDetails.add(detail);
                }
            }
            purchaseEnquiryDetailService.saveBatch(purchaseEnquiryForm.getPurchaseEnquiryDetails());
            purchaseRequisitionsDetailService.updateBatchById(purchaseRequisitionsDetails);
        }
        return i;
    }

    @Override
    public List<PurchaseEnquiryVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<PurchaseEnquiryVO> pageList4app(Page page, Map<String, Object> params) {
        List<PurchaseEnquiryVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public PurchaseEnquiryVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }
}
